package com.szzg10.thinking.chapter18;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class LargeMappedFile {

	private static final int LENGTH = 0x8FFFFFF;
	
	public static void main(String[] args) throws IOException {
		File file = new File(BasicFileOutput.FILE_NAME_OUT);
		RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
		FileChannel channel = randomAccessFile.getChannel();
		MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, LENGTH);
		
		for(int i = 0; i < LENGTH; i++) {
			mappedByteBuffer.put((byte)'x');
		}
		System.out.println("Finished writing!!!");
		
		for(int i = LENGTH / 2; i < (LENGTH / 2 + 6); i++) {
			System.out.println((char)mappedByteBuffer.get(i));
		}
		
		channel.close();
		randomAccessFile.close();
	}
}
